package com.ruoyi.web.Interceptord;

import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)

public class TraceInterceptor implements HandlerInterceptor {


    @Autowired
    private RedisTemplate redisTemplate;

    public static String trimChar(String sourceStr, char trimStr) {
        int len = sourceStr.length();
        int st = 0;
        char[] val = sourceStr.toCharArray();

        while ((st < len) && (val[st] <= trimStr)) {
            st++;
        }
        while ((st < len) && (val[len - 1] <= trimStr)) {
            len--;
        }
        return ((st > 0) || (len < sourceStr.length())) ? sourceStr.substring(st, len) : sourceStr;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //放行
//        if("/login".equals(request.getHeader("url"))  ||  "/getInfo".equals(request.getHeader("url")) || request.getHeader("url").contains("getPublicKey") || request.getRequestURI().contains("captchaImage")){//|| request.getHeader("url") == null
//            return true;
//        }
//        response.setHeader("X-frame-options","DENY");
////////        System.out.println(request.getHeader("url"));
//////
////
//        response.setHeader("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline' cdn.example.com");
////
////
//        response.setHeader("x-content-type-options","nosniff");
//        //response.setHeader("x-frame-options","DENY");
//        response.setHeader("x-xss-protection","1; mode=block");
//        //response.setHeader("x-frame-options","SAMEORIGIN");
////
        MDC.put("threadId",String.valueOf(Thread.currentThread().getId()));
        //traceId
        MDC.put("traceId" , TraceContext.traceId());
        //跨度id
        MDC.put("spanId",String.valueOf(TraceContext.spanId()));
        //系统编号
        MDC.put("SYSCODE","QLL");
        //请求时间
        MDC.put("reqTime",new SimpleDateFormat("YYYY-MM-DD HH:MM:SS sss").format(new Date()));
        //请求uri
        MDC.put("reqURI",request.getRequestURI());
        //请求url
        MDC.put("reqURL",request.getRequestURL().toString());
////
////        //判断请求头的是
//        String Origin = request.getHeader("Origin");
//        if(Origin != null && !"".equals(Origin)){
//            //漏洞，不能用明文 ，修改到配置文件中 mgb
//            List<String> list = Arrays.asList(urlParams.split(","));
//            //ip 集合
//            boolean isContext = false;
//            for(String a : list){
//                if(Origin.contains(a)){
//                    isContext = true;
//                    break;
//                }
//            }
//
//            if(isContext){
//
//                response.setHeader("Access-Control-Allow-Origin", Origin);
//                response.setHeader("Access-Control-Allow-Methods", "POST, GET");
//                response.setHeader("Access-Control-Max-Age", "3600");
//                response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
//                log.info("正确的源地址");
//            }else {
//                log.info("错误的源地址");
//                throw new OriginErrorException();
//
//            }
//        }else{
//          if(request.getRequestURI().contains("getRouters"))  {
//
//          }else{
//              return true;
//          }
//        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        MDC.clear();
    }
}
